<?php
include_once(dirname(realpath(__FILE__)).'/../global.php');

class comment_util_comment {

	/**
	 * 获取资源最新的$num条评论
	 *
	 * @param unknown_type $app
	 * @param unknown_type $controller
	 * @param unknown_type $action
	 * @param unknown_type $id
	 * @param unknown_type $num
	 * @return unknown
	 */
	static function getNewComment($app,$controller,$action,$id,$num){
		$res_id = md5($app.$controller.$action.$id);
		return  Model::factory('comment_model_data')->getNewComment($res_id,$num);
		//缓存评论
		/*$key	=	md5($res_id.$num);
		if(!($res = Cache::getInstance()->load($key))){
			$res = Model::factory('comment_model_data')->getNewComment($res_id,$num);
			Cache::getInstance()->save(serialize($res),$key,array(),3600);
			return $res;
		}else{
			return unserialize($res);
		}*/
	}
	static	function getCommentCount($app,$cotroller,$action,$id){
				$res_id = md5($app.$cotroller.$action.$id);
				$res = Model::factory('comment_model_resources');
				$result = $res->check_res2($res_id);
				return $result['comment_num'];
		}
	static function getCommentContent($app,$cotroller,$action,$id,$uid){
		
				$res_id = md5($app.$cotroller.$action.$id);
				$data = Model::factory('comment_model_data');
				$result = $data->getCommentContent($res_id,$uid);
				$result['content'] =
				preg_replace($_SERVER['config']['smile']['searcharray'],$_SERVER['config']['smile']['replacearray'],$result['content']);	
				//var_dump($result['content']); exit;	
				return $result;
		}
	
	static function getNewState($uid){
				$state  = Model::factory('comment_model_state');
				$result = $state->read_a_data($uid);
				$result['feel']=
				preg_replace($_SERVER['config']['smile']['searcharray'],$_SERVER['config']['smile']['replacearray'],$result['feel']);
				return $result;
	}
	static  function getStateCount($uid){
				$state = Model::factory('comment_model_state');
				return $state->count_state($uid);
	}
	//取得一段时间内的状态数
	static function getStateCountTime($uid,$time){
				$state = Model::factory('comment_model_state');
				return  $state->getCountTime($uid,$time);
	}
	
	static function makeTheCode($app,$cotroller,$action,$id){
				$key = '178.comsShuiyRdkfw007';
				$code = md5(md5($key).$app.$cotroller.$action.$id); 
				return $code;
	}
	static function explainTheCode($app,$control,$action,$id,$code){
				$key = '178.comsShuiyRdkfw007';
				$code_ture = md5(md5($key).$app.$control.$action.$id);
				if($code_ture === $code) return true;
				else return false;
	}
	
	/* 动态评论接口 */
	 // 取得评论内容  $rid 模块id $id 资源id $uid 用户id 
	static function getCommentByMid($mid,$id,$uid){
		
				$data = Model::factory('comment_model_data');
				$data ->getCommentByMid($mid,$id,$uid);				
	}
	
	/* 评论外部接口 添加资源 提交一条评论 */
	// $array['app'] $array['control'] $array['action'], $array['id'] , $array['uid'] , $array['username'] $array['content']
	static function  submitCommentAddRes($array) {
				$res	=	Model::factory('comment_model_resources');
				$post	=	Model::factory('comment_model_post');
				$thread	=	Model::factory('comment_model_thread');				

				$res_name = isset($_SERVER['config']['comments'][$array['app']][$array['control']])? $_SERVER['config']['comments'][$array['app']][$array['control']] : 0 ;
				if ($res_name) {					
					$res_id		=	md5($array['app'].$array['control'].$array['action'].$array['id']);
					$arguments	=	array('res_id'=>$res_id, 'res_name'=>$res_name,'uid'=>$array['uid'],'app'=>$array['app'], 'control'=>$array['control'], 'act'=>$array['action'],'id'=>$array['id'],'deleted'=>0);
					$result = $res->check_res($res_id);					
					if (!$result) {
						$result = $res->add_resources($arguments);
					}
					if ($result) {
						$array['res_id']	= $res_id;
						$array['author_id'] = Controller::$login['uid'];
						$array['author_name']	= Controller::$login['nickname'];
						$array['type']			=  1 ;
						$thread_id = $thread->add_thread($array);
						if ($thread_id) {
							$res->update_res_comment($res_id,'');
							$add_result = $post->add_post($array,$thread_id);
							if ($add_result) {								
								$key	=	md5($res_id.'178.com');
								Cache::getInstance()->remove($key);
								Cache::getInstance()->remove($res_id);
								return $add_result;
							} else {
								return false;
							}
							
						}
					}

			}
		
	}
	
	
		
}
